﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.ISerivces;
using DWF.Studio.Models;
using DWF.Studio.Project;
using DWF.Studio.Share.Serialization;
using DWF.Studio.Share.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.Services
{
    internal class ProjectParamDetailSerivce : IProjectParamDetailSerivce
    {
        private ProjectManager _projectManager = null;

        public ProjectParamDetailSerivce()
        {
            _projectManager = ProjectManager.Instance;
        }

        public ProjectParamDetail GetProjectParamDetail()
        {
            ProjectParamDetail detail = null;
            if (_projectManager.CurrentProject != null)
            {
                var projectPath = _projectManager.CurrentProject.FullName;
                detail = getParamDetail(projectPath);
            }

            return detail;
        }

        public void SaveProjectParamDetail(ProjectParamDetail projectParamDetail)
        {
            if (_projectManager.CurrentProject != null)
            {
                var projectPath = _projectManager.CurrentProject.FullName;
                saveParamDetail(projectPath, projectParamDetail);
            }
        }
        public void SetChangeParam(List<ProjectParam> projectParam)
        {
            if (_projectManager.CurrentProject != null)
            {
                var projectPath = _projectManager.CurrentProject.FullName;
                var detail = getParamDetail(projectPath);
                if (projectParam != null)
                {
                    GlobalActionHandler.SetPutParams.Invoke(new TagEventArgs<List<ProjectParam>>(projectParam));
                }
            }
        }
        public void SetInputParam(ProjectParam projectParam)
        {
            if (_projectManager.CurrentProject != null)
            {
                var projectPath = _projectManager.CurrentProject.FullName;
                var detail = getParamDetail(projectPath);
                if (projectParam != null)
                {
                    GlobalActionHandler.SetPutParam.Invoke(new TagEventArgs<ProjectParam, int>(projectParam, 0));
                }
            }
        }

        public void SetOutputParam(ProjectParam projectParam)
        {
            if (_projectManager.CurrentProject != null)
            {
                var projectPath = _projectManager.CurrentProject.FullName;
                var detail = getParamDetail(projectPath);
                if (projectParam != null)
                {
                    GlobalActionHandler.SetPutParam.Invoke(new TagEventArgs<ProjectParam, int>(projectParam, 1));
                }
            }
            //throw new NotImplementedException();
        }
       
        private ProjectParamDetail getParamDetail(string projectPath)
        {
            var detailPath = System.IO.Path.Combine(projectPath, StaticResource.ProjectParams);

            ProjectParamDetail detail = null;

            if (FileHelper.CheckFileExisting(detailPath))//检查配置文件是否存在，如果存在这加载，否者新建。
            {
                detail = YamlHelper.Load<ProjectParamDetail>(detailPath, true);
            }

            if (detail == null)
            {
                detail = new ProjectParamDetail()
                {
                    Input = new List<ProjectParam>(),
                    Output = new List<ProjectParam>()
                };
                saveParamDetail(projectPath, detail);
            }

            return detail;
        }

        private void saveParamDetail(string projectPath, ProjectParamDetail detail)
        {
            var settingPath = System.IO.Path.Combine(projectPath, StaticResource.ProjectParams);

            YamlHelper.Save(detail, settingPath, true);
        }
    }
}
